/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lily
 * Date: 2024-09-19
 * Time: 16:44
 */
public class find_PeakElement {
    public static int findPeakElement(int[] nums) {
        if (nums.length==1){
            return 0;
        }

        int left = 0, right = nums.length - 1;

        if (nums[left] > nums[++left]) {
            return 0;
        }

        if (nums[right] > nums[--right]) {
            return nums.length-1;
        }
        while (left < right) {
            int mid = left + (right - left+1) / 2;
            if (nums[mid - 1] <= nums[mid]) {
                left = mid;
            }else {
                right = mid-1;
            }
        }

        return right;
    }

    public static void main(String[] args) {
        int[] nums=new int[]{1,3,2,1};
        System.out.println(findPeakElement(nums));
    }
}
